﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <ActivityDesignerTheme(GetType(EventSinkDesignerTheme))> _
    Friend Class HandleExternalEventActivityDesigner
        Inherits ActivityDesigner
        ' Methods
        Private Sub AddRemoveCorrelationToken(ByVal interfaceType As Type, ByVal properties As IDictionary, ByVal corrRefProperty As Object)
            If (Not interfaceType Is Nothing) Then
                Dim customAttributes As Object() = interfaceType.GetCustomAttributes(GetType(CorrelationProviderAttribute), False)
                Dim objArray2 As Object() = interfaceType.GetCustomAttributes(GetType(CorrelationParameterAttribute), False)
                If ((customAttributes.Length <> 0) OrElse (objArray2.Length <> 0)) Then
                    If Not properties.Contains("CorrelationToken") Then
                        properties.Add("CorrelationToken", corrRefProperty)
                    End If
                    Return
                End If
            End If
            If properties.Contains("CorrelationToken") Then
                properties.Remove("CorrelationToken")
            End If
        End Sub

        Protected Overrides Sub OnActivityChanged(ByVal e As ActivityChangedEventArgs)
            MyBase.OnActivityChanged(e)
            If (Not e.Member Is Nothing) Then
                If (e.Member.Name = "InterfaceType") Then
                    If (Not MyBase.Activity.Site Is Nothing) Then
                        Dim newValue As Type = [TryCast](e.NewValue,Type)
                        If (Not newValue Is Nothing) Then
                            New ExternalDataExchangeInterfaceTypeFilterProvider(MyBase.Activity.Site).CanFilterType(newValue, True)
                        End If
                        Dim activity As Activity = e.Activity
                        PropertyDescriptorUtils.SetPropertyValue(MyBase.Activity.Site, TypeDescriptor.GetProperties(MyBase.Activity).Item("EventName"), MyBase.Activity, String.Empty)
                        If (DirectCast(MyBase.Activity.Site.GetService(GetType(IExtendedUIService)), IExtendedUIService) Is Nothing) Then
                            Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IExtendedUIService).FullName }))
                        End If
                    End If
                ElseIf ((e.Member.Name = "EventName") AndAlso TypeOf e.Activity Is HandleExternalEventActivity) Then
                    [TryCast](e.Activity,HandleExternalEventActivity).ParameterBindings.Clear
                End If
                If (((e.Member.Name = "InterfaceType") OrElse (e.Member.Name = "EventName")) OrElse (e.Member.Name = "CorrelationToken")) Then
                    TypeDescriptor.Refresh(e.Activity)
                End If
            End If
        End Sub

        Protected Overrides Sub PreFilterProperties(ByVal properties As IDictionary)
            MyBase.PreFilterProperties(properties)
            Dim corrRefProperty As Object = properties.Item("CorrelationToken")
            Dim activity As HandleExternalEventActivity = [TryCast](MyBase.Activity,HandleExternalEventActivity)
            Me.AddRemoveCorrelationToken(activity.InterfaceType, properties, corrRefProperty)
            Dim interfaceType As Type = activity.InterfaceType
            If (Not interfaceType Is Nothing) Then
                Me.AddRemoveCorrelationToken(interfaceType, properties, corrRefProperty)
                activity.GetParameterPropertyDescriptors(properties)
            End If
        End Sub

    End Class
End Namespace

